package smartagdemo.service.impl;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import smartagdemo.bean.HumidityData;
import smartagdemo.bean.LightData;
import smartagdemo.bean.TemperatureData;
import smartagdemo.bean.TestData;
import smartagdemo.service.ScheduleService;
import smartagdemo.service.UploadService;
import smartagdemo.service.ViewService;
import smartagdemo.tool.CalculateAVG;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ViewService viewService;
    @Autowired
    private UploadService uploadService;

    private Logger logger= LoggerFactory.getLogger(ScheduleServiceImpl.class);
    int count1 = 1;
    int count2 = 1;
    int count3 = 1;

//    @Scheduled(fixedRate = 10000)
//    @Async
//    public void jobFixedRate(){
//        List<TestData> templeTestDataList=new ArrayList<>();
//        Thread.currentThread().setName("间隔执行");
//        logger.info("{}.执行第{}次",Thread.currentThread().getName(),count1++);
////获取前一天和当天日期
//        Date day=new Date();
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//        Calendar date = Calendar.getInstance();
//        date.setTime(day);
//        String date2=df.format(date.getTime());
//        date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
//        String date1=df.format(date.getTime());
//        System.out.println(date1);
//        System.out.println(date2);
////获取前一天的所有数据并求平均
//        templeTestDataList=viewService.viewTest(null ,null);
//        TestData testDataTemple=CalculateAVG.CalculateTestAVG(templeTestDataList);
//        System.out.println(testDataTemple.getHumidityData());
//        System.out.println(testDataTemple.getTemperatureData());
//        System.out.println(testDataTemple.getLightData());
////上传数据
//        uploadService.uploadDateTest(testDataTemple.getHumidityData(),testDataTemple.getTemperatureData(),testDataTemple.getLightData());
//    }

    @Scheduled(cron = "0 0 01 ? * * ")
//    @Scheduled(fixedRate = 10000)
    @Async
    public void dailyTask(){
        Thread.currentThread().setName("间隔执行");
        logger.info("{}.执行第{}次",Thread.currentThread().getName(),count2++);

        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar date = Calendar.getInstance();
        date.setTime(day);
        date.set(Calendar.HOUR_OF_DAY,0);
        date.set(Calendar.MINUTE,0);
        date.set(Calendar.SECOND,0);
        date.set(Calendar.MILLISECOND,0);
        String date2=df.format(date.getTime());
        Timestamp tDate2=Timestamp.valueOf(date2);
        System.out.println(tDate2);

        date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
        String date1=df.format(date.getTime());
        Timestamp tDate1=Timestamp.valueOf(date1);
        System.out.println(tDate1);

        List<HumidityData> humidityDataList=viewService.viewHumidityData(tDate1,tDate2);
        List<TemperatureData> temperatureDataList=viewService.viewTemperatureData(tDate1,tDate2);
        List<LightData> lightDataList=viewService.viewLightData(tDate1,tDate2);
        uploadService.uploadDateHumidity(CalculateAVG.CalculateHumidityAVG(humidityDataList));
        uploadService.uploadDateTemperature(CalculateAVG.CalculateTemperatureAVG(temperatureDataList));
        uploadService.uploadDateLight(CalculateAVG.CalculateLightAVG(lightDataList));
    }
}
